package com.hd.crdao.activity;

import android.text.TextUtils;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.LocationForAreaBean;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.view.ExpandHeadView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/7/5 0005
 */
public class MapShowActivity extends BaseSecondActivity {

    MapView mMapView;
    @BindView(R.id.mapshow_head)
    ExpandHeadView mMapshowHead;
    public BaiduMap mBaiduMap;


    @Override
    protected int setContentView() {
        return R.layout.activity_showmap;

    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mMapshowHead;
    }


    @Override
    protected void initView() {
        mMapView = (MapView) findViewById(R.id.mapview);
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        String point = getIntent().getStringExtra("point");
        String allAddress = getIntent().getStringExtra("allAddress");
        if (TextUtils.isEmpty(point)) {
            TransLocation(allAddress);
        } else {
            initBaidu(point);
        }


    }

    private void initBaidu(String point) {
        String[] split = point.split(",");
        LatLng latLng = new LatLng(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
        MapStatusUpdate update = MapStatusUpdateFactory.zoomTo(18.0f);
        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng);
        mBaiduMap.animateMapStatus(update);
        mBaiduMap.animateMapStatus(mapStatusUpdate);
        initLocation(split);
    }

    private void initLocation(String[] split) {
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory
                .fromResource(R.drawable.map_b_2x);

        MyLocationConfiguration config = new MyLocationConfiguration(
                MyLocationConfiguration.LocationMode.NORMAL, true, bitmapDescriptor);
        mBaiduMap.setMyLocationConfigeration(config);

        MyLocationData locData = new MyLocationData.Builder()
                .accuracy(360)
                .direction(100)
                .latitude(Double.parseDouble(split[0]))
                .longitude(Double.parseDouble(split[1]))
                .build();
        mBaiduMap.setMyLocationData(locData);

        LatLng latLng = new LatLng(Double.parseDouble(split[0]), Double.parseDouble(split[1]));
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.map_b_2x);
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(latLng)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);


    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    private void TransLocation(final String address) {
       /* address:福建省福州市鼓楼区树汤路51号
        output:json
        ak:ogb7qtmDrZ9KyqqTTOEBPcpNDMeiqMhn
        callback:showLocation*/
        String url = "http://api.map.baidu.com/geocoder/v2/";
        OkHttpUtils.post().url(url)
                .addParams("output", "json")
                .addParams("address", address)
                .addParams("ak", "ogb7qtmDrZ9KyqqTTOEBPcpNDMeiqMhn")
                .addParams("callback", "showLocation")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("网络不佳,请稍后重试");
            }

            @Override
            public void onResponse(String response, int id) {
                if (response.contains("<?xml version=\"1.0\" encoding=\"utf-8\"?>")) {
                    MyToast.show("由于地址有误,无法显示正确位置");
                } else {
                    LocationForAreaBean locationForAreaBean = new Gson().fromJson(response, LocationForAreaBean.class);
                    if (locationForAreaBean.getStatus() == 0) {
                        LocationForAreaBean.ResultBean.LocationBean location = locationForAreaBean.getResult().getLocation();
                        initBaidu(location.getLat() + "," + location.getLng());
                    } else {
                        MyToast.show("由于地址有误,无法显示正确位置");
                    }
                }
            }
        });

    }
}



